Option Strict Off
Option Explicit On
Friend Class frmTryConnect
	Inherits System.Windows.Forms.Form
  Dim ind As Integer
  Dim tempconnect As Short

  Private Sub frmTryConnect_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Timer1.Enabled = False
    'Form1.SockClient.Close
  End Sub
	Private Sub frmTryConnect_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		GetinitWaitting()
	End Sub
	Public Sub GetinitWaitting()
		If Not Form1.TryConnect Then
      Text = My.Resources.MSG_WAITING_OPP
		Else
      Text = My.Resources.MSG_TRY_CONNECT & Form1.HostName
		End If
    ind = 4
		tempconnect = 0
		Timer1.Interval = 100
		ProgressBar1.Value = 0
		ProgressBar1.Maximum = 10000
	End Sub

	Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
    'On Error Resume Next
    If (tempconnect = ProgressBar1.Maximum AndAlso _
     Form1.SockClient.GetState <> Winsock_Control.WinsockStates.Connected) OrElse _
     Form1.SockClient.GetState = Winsock_Control.WinsockStates.Error Then ' give-up trying to connect
      MessageBox.Show(My.Resources.MSG_CANNOT_CONNECT)
      FileClose()
      Timer1.Enabled = False : tempconnect = 0 : Form1.Connected = False
      Form1.SockClient.Close()
      Me.Close()
      Exit Sub
    Else
      ' retry to connect at 20 sec intervals
      If tempconnect > 0 And tempconnect Mod 200 = 0 And Form1.TryConnect Then
        If Form1.SockClient.GetState <> Winsock_Control.WinsockStates.Connected Then
          NameNIP.Connect()
        End If
      Else
        If Form1.SockClient.GetState = Winsock_Control.WinsockStates.Connected Then ' connected !!
          Timer1.Enabled = False : tempconnect = 0
          Me.Close()
          Exit Sub
        End If
      End If
    End If
		tempconnect = tempconnect + 1
		ProgressBar1.Value = tempconnect Mod ProgressBar1.Maximum
	End Sub
End Class